/*
 * Copyright 2002-2006 the original author or authors. Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions and limitations under the
 * License.
 */
package pikes.xml;

import java.io.IOException;
import java.io.StringWriter;

import junit.framework.TestCase;
import pikes.core.Printable;
import pikes.xml.XMLVisitor;
import pikes.xml.XMLWriter;

public class XMLWriterTestCase extends TestCase {

	public void testXMLDeclaration() throws Exception {
		StringWriter writer = new StringWriter();
		XMLVisitor xmlWriter = new XMLWriter(writer);
		xmlWriter.xmlDeclaration("ver");
		writer.flush();
		assertEquals("<?xml version=\"ver\"?>", writer.toString());
	}

	public void testXMLDeclarationWithencoding() throws Exception {
		StringWriter writer = new StringWriter();
		XMLVisitor xmlWriter = new XMLWriter(writer);
		xmlWriter.xmlDeclaration("ver", "dec");
		writer.flush();
		assertEquals("<?xml version=\"ver\" encoding=\"dec\"?>", writer.toString());
	}

	public void testStartElement() throws Exception {
		StringWriter writer = new StringWriter();
		XMLVisitor xmlWriter = new XMLWriter(writer);
		xmlWriter.openingFirstTag("name");
		writer.flush();
		assertEquals("<name", writer.toString());
	}

	public void testStartElementWithPrefix() throws Exception {
		StringWriter writer = new StringWriter();
		XMLVisitor xmlWriter = new XMLWriter(writer);
		xmlWriter.openingFirstTag("prefix", "name");
		writer.flush();
		assertEquals("<prefix:name", writer.toString());
	}

	public void testAppendAttribute() throws Exception {
		StringWriter writer = new StringWriter();
		XMLVisitor xmlWriter = new XMLWriter(writer);
		xmlWriter.attribute("name", "value");
		writer.flush();
		assertEquals(" name=\"value\"", writer.toString());
	}

	public void testAppendAttribute1() throws Exception {
		StringWriter writer = new StringWriter();
		XMLVisitor xmlWriter = new XMLWriter(writer);
		xmlWriter.attribute("name", new Printable() {

			public void print(Appendable appendable) throws IOException {
				appendable.append("value");
			}

		});
		writer.flush();
		assertEquals(" name=\"value\"", writer.toString());
	}

	public void testCloseElement() throws Exception {
		StringWriter writer = new StringWriter();
		XMLVisitor xmlWriter = new XMLWriter(writer);
		xmlWriter.closingFirstTag("name", false);
		writer.flush();
		assertEquals("/>", writer.toString());
	}

	public void testCloseElement2() throws Exception {
		StringWriter writer = new StringWriter();
		XMLVisitor xmlWriter = new XMLWriter(writer);
		xmlWriter.closingFirstTag("name", true);
		writer.flush();
		assertEquals(">", writer.toString());
	}

	public void testCloseElementByPrintingClosingTagForEmpty() throws Exception {
		StringWriter writer = new StringWriter();
		XMLVisitor xmlWriter = new XMLWriter(writer, true);
		xmlWriter.closingFirstTag("name", false);
		writer.flush();
		assertEquals("></name>", writer.toString());
	}

	public void testCloseElement2ByPrintingClosingTagForEmpty() throws Exception {
		StringWriter writer = new StringWriter();
		XMLVisitor xmlWriter = new XMLWriter(writer, true);
		xmlWriter.closingFirstTag("name", true);
		writer.flush();
		assertEquals(">", writer.toString());
	}

	public void testWriteClosingTag() throws Exception {
		StringWriter writer = new StringWriter();
		XMLVisitor xmlWriter = new XMLWriter(writer);
		xmlWriter.closeSecondTag("name");
		writer.flush();
		assertEquals("</name>", writer.toString());
	}

	public void testWriteText() throws Exception {
		StringWriter writer = new StringWriter();
		XMLVisitor xmlWriter = new XMLWriter(writer);
		xmlWriter.text("text");
		writer.flush();
		assertEquals("text", writer.toString());
	}

	public void testWriteText1() throws Exception {
		StringWriter writer = new StringWriter();
		XMLVisitor xmlWriter = new XMLWriter(writer);
		xmlWriter.text(new Printable() {

			public void print(Appendable appendable) throws IOException {
				appendable.append("text");
			}

		});
		writer.flush();
		assertEquals("text", writer.toString());
	}

	/*
	 * public void testWriteTextWithLessThanCharacter() throws Exception { StringWriter writer = new StringWriter();
	 * XMLWriter xmlWriter = new XMLWriter(writer); xmlWriter.text("<"); writer.flush(); assertEquals("&lt;",
	 * writer.toString()); } public void testWriteTextWithGreaterThanCharacter() throws Exception { StringWriter writer =
	 * new StringWriter(); XMLWriter xmlWriter = new XMLWriter(writer); xmlWriter.text(">"); writer.flush();
	 * assertEquals("&gt;", writer.toString()); }
	 */
}
